﻿using UnityEngine;
using System.Collections;
using PBMessage;
public class UILogin : BaseWidget
{
    public UIInput AccountInput;
    public UIInput NickNameInput;
    public UIInput PwdInput;
    public UILabel InfoText;
	public GameObject Btn_Register;
	public GameObject Btn_AccountLogin;
	public GameObject LoginView;
	public GameObject RegisterView;
	public GameObject Btn_RegisterAccount;
	public GameObject Btn_BackToLogin;

    public UIInput LoginAccountInput;
    public UIInput LoginPwdInput;
    public UILabel LoginInfoLabel;

    PlayerModel playerModel;
    public override void OnLoad()
    {
        //Game.Net.NetWork.ConnectToServer("123.59.133.195",54161,1000);
        Game.Net.NetWork.ConnectToServer("127.0.0.1", 54161, 1000);
        playerModel = Game.Model.NewGet<PlayerModel>();
        Init();
    }

	public void Init()
	{
		LoginView.gameObject.SetActive(true);
		RegisterView.gameObject.SetActive(false);
		UIEventListener.Get(Btn_Register).onClick = ClickRegister;
		UIEventListener.Get(Btn_AccountLogin).onClick = OnClickLogin;
		UIEventListener.Get(Btn_RegisterAccount).onClick = OnClickRegAccount;
		UIEventListener.Get(Btn_BackToLogin).onClick = ClickBackToLogin;
	}

    public void OnClickRegAccount(GameObject go) 
    {
        if (AccountInput.label.text.Trim() == "" || NickNameInput.label.text.Trim() == "" || PwdInput.label.text.Trim() == "") 
        {
            InfoText.text = "不可为空";
            return;
        }
        Game.Packet.SendRegAccount(AccountInput.label.text.Trim(), NickNameInput.label.text.Trim(), PwdInput.label.text.Trim());
    }

    public void ServerRet_RegAccount(SC_RegAccount data)
    {
        if (data.Type == 0)
        {
            InfoText.text = "注册成功";
        }
        else if (data.Type == 1) 
        {
            InfoText.text = "注册失败 用户名已经存在";
        }
    }

    public void OnClickLogin(GameObject go) 
    {
        if (LoginAccountInput.label.text.Trim() == "" || LoginPwdInput.label.text.Trim() == "")
        {
            InfoText.text = "不可为空";
            return;
        }
        playerModel.Account = LoginAccountInput.label.text.Trim();
        Game.Packet.SendLogin(LoginAccountInput.label.text.Trim(),LoginPwdInput.label.text.Trim());
	}

	public void ClickRegister(GameObject go)
	{
		LoginView.gameObject.SetActive(false);
		RegisterView.gameObject.SetActive(true);
	}

	public void ClickBackToLogin(GameObject go)
	{
		LoginView.gameObject.SetActive(true);
		RegisterView.gameObject.SetActive(false);
	}

    public void ServerRet_Login(SC_Login data) 
    {
        if (data.Type == 0) 
        {
            LoginInfoLabel.text = "登录成功 昵称:"+data.NickName;
            PlayerModel playerModel = Game.Model.NewGet<PlayerModel>();
            playerModel.NickName = data.NickName;
            playerModel.UserId = data.UserId; 
            Game.UI.Close("Login");
			Game.UI.Show("MainView");
        }
        else 
        {
            LoginInfoLabel.text = "账号或密码错误";
        }
    }
}
